package com.chnbst.bstyun.eos;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chnbst.bstyun.core.common.ReadProperties;
import com.chnbst.bstyun.model.pojo.extend.GetUser;
import com.chnbst.bstyun.model.pojo.extend.UserDelete;
import com.chnbst.bstyun.model.pojo.extend.UserPasswd;
import com.chnbst.bstyun.model.pojo.extend.UserRegister;
import com.chnbst.bstyun.model.pojo.extend.UserUpdate;

import net.sf.json.JSONObject;

/**
 * 用户操作
 * 
 * @作者 yeabow
 * @创建日期 Jul 12, 2011 4:52:08 PM
 * @版本 v1.0.0
 */
@Operator("user")
@Component
public class UserOperator extends ServiceOperator {
	@Autowired
	private ReadProperties read;
	
	private UserOperator() {
	}

	private static class UserOperatorHolder {
		static UserOperator userOperator = new UserOperator();
	}

	/**
	 * 用户注册
	 * 
	 * @param userRegister
	 *            注册实体
	 * @return 当前注册用户的编号
	 *             用户注册异常
	 */
	public String register(UserRegister userRegister){
		System.out.println("＜－－－－－－－startTime:"+dateFormat.format(new Date())+" - 执行即时通用户注册接口！－－－－－－－＞");
		//返回值
		String isReturn = "OK";
		try {
			JSONObject json = JSONObject.fromObject(userRegister);
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("jsonString", URLEncoder
						.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(read.getInterfaceUrl(),params);
			if (log.isDebugEnabled()) {
				log.debug("执行结果返回值 : "+rs);
			}
			//判断当前返回数据是否访问受限!
			String[] str = rs.split("访问受限");
			//根据截取返回数据判断是否访问受限!
			if (str.length>1) {
				return "【即时通接口错误：链接异常】";
			}
			Integer uid = Integer.valueOf(rs);
			if (uid <= 0) {
				switch (uid) {
					case -1:
						isReturn = "【即时通接口错误：没有指定用户密码!】";
						break;
					case -2:
						isReturn = "【即时通接口错误：别名包含不允许注册的词语!】";
						break;
					case -3:
						isReturn = "【即时通接口错误：别名已经存在!】";
						break;
					case -4:
						isReturn = "【即时通接口错误：Email格式有误!】";
						break;
					case -5:
						isReturn = "【即时通接口错误：Email不允许注册】";
						break;
					case -6:
						isReturn = "【即时通接口错误：Email不允许登录，已经被其他账号使用】";
						break;
					case -7:
						isReturn = "【即时通接口错误：账号已经存在!】";
						break;
					case -8:
						isReturn = "【即时通接口错误：账号为必输项】";
						break;
					case -9:
						isReturn = "【即时通接口错误：手机号码不允许登录,已经被其他账号使用.】";
						break;
					case -10:
						isReturn = "【即时通接口错误：登录号必须为数字.】";
						break;
					case -11:
						isReturn = "【即时通接口错误：登录号必须大于等于10000,且不能为手机号码】";
						break;
					case -12:
						isReturn = "【即时通接口错误：没有足够的号码进行分配,请在联系管理员进行操作】";
						break;
				}
			}
			System.out.println("<－－－－－－－endTime:"+dateFormat.format(new Date())+" - 执行即时通注册结果返回值 : ["+uid+"] "+isReturn+"－－－－－－－>");
		} catch (UnsupportedEncodingException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (ConnectException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (IOException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		}
		return isReturn;
	}


	/**
	 * 获取用户信息
	 * @param getUser
	 * @return
	 */
	public Integer get(GetUser getUser){
		Integer isUser= 1;
		try {
			JSONObject json = JSONObject.fromObject(getUser);
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("jsonString", URLEncoder
						.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(read.getInterfaceUrl(),params);
			if (log.isDebugEnabled()) {
				log.debug(rs);
			}
			
			JSONObject r = JSONObject.fromObject(rs);
			if (!r.getBoolean("success")) {
				int status = r.getInt("error");
				isUser=status;
				System.out.println("查询即时通用户返回值: "+status);
			} else {
				if (r.has("result")) {
					JSONObject result = r.getJSONObject("result");
					System.out.println(result.toString());
				} else {
					return isUser;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return isUser;
	}
	
	/**
	 * 修改密码
	 * @param userPasswd
	 * @return
	 */
	public String passwd(UserPasswd userPasswd) {
		System.out.println("＜－－－－－－－startTime:"+dateFormat.format(new Date())+" - 执行即时通用户密码更新接口！－－－－－－－＞");
		//返回值
		String isReturn = "OK";
		try {
			JSONObject json = JSONObject.fromObject(userPasswd);
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("jsonString", URLEncoder
						.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(read.getInterfaceUrl(),params);
			if (log.isDebugEnabled()) {
				log.debug(rs);
			}
			//判断当前返回数据是否访问受限!
			String[] str = rs.split("访问受限");
			//根据截取返回数据判断是否访问受限!
			if (str.length>1) {
				return "【即时通接口错误：链接异常】";
			}
			Integer status = Integer.valueOf(rs);
			if (status <= 0) {
				switch (status) {
				case 0:
					isReturn = "【即时通接口错误：没有提供相应的参数!】";
					break;
				case -1:
					isReturn = "【即时通接口错误：服务器处理失败!】";
					break;
				case -2:
					isReturn = "【即时通接口错误：帐号提供有误!】";
					break;
				case -3:
					isReturn = "【即时通接口错误：密码不允许修改为空】";
					break;
				}
			}
			System.out.println("<－－－－－－－endTime:"+dateFormat.format(new Date())+" - 执行即时通密码更新结果返回值 : ["+status+"] "+isReturn+" －－－－－－－>");
		} catch (UnsupportedEncodingException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (ConnectException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (IOException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		}
		return isReturn;
	}
	

	/**
	 * 用户更新
	 * @param user
	 * @return
	 */
	public String update(UserUpdate user) {
		System.out.println("＜－－－－－－－ startTime:"+dateFormat.format(new Date())+" - 执行即时通用户更新接口！－－－－－－－＞");
		//返回值
		String isReturn = "OK";
		try {
			JSONObject json = JSONObject.fromObject(user);
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("jsonString", URLEncoder
						.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(read.getInterfaceUrl(),params);
			if (log.isDebugEnabled()) {
				log.debug(rs);
			}
			//判断当前返回数据是否访问受限!
			String[] str = rs.split("访问受限");
			//根据截取返回数据判断是否访问受限!
			if (str.length>1) {
				return "【即时通接口错误：链接异常】";
			}
			Integer status = Integer.valueOf(rs);
			if (status <= 0) {
				switch (status) {
				case 0:
					isReturn = "【即时通接口错误：没有提供相应的参数】";
					break;
				case -1:
					isReturn = "【即时通接口错误：帐号提供有误】";
					break;
				case -2:
					isReturn = "【即时通接口错误：帐号提供有误】";
					break;
				case -3:
					isReturn = "【即时通接口错误：别名不能为空,且大于0】";
					break;
				case -4:
					isReturn = "【即时通接口错误：用户编号不能为空】";
					break;
				case -5:
					isReturn = "【即时通接口错误：登录号不能为空】";
					break;
				case -6:
					isReturn = "【即时通接口错误：Email格式有误】";
					break;
				case -7:
					isReturn = "【即时通接口错误：帐号暂用,不允许登录】";
					break;
				case -8:
					isReturn = "【即时通接口错误：IM帐号已经被占用】";
					break;
				}
			}

			System.out.println("<－－－－－－－endTime:"+dateFormat.format(new Date())+" - 执行即时通用户更新结果返回值 : ["+status+"] "+isReturn+"－－－－－－－>");
		} catch (UnsupportedEncodingException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (ConnectException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (IOException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		}
		return isReturn;
	}
	

	/**
	 * 用户禁启用设置
	 * @param user
	 * @return
	 */
	/*public String UserRestart(UserRestartEntity user) {
		System.out.println("＜－－－－－－－ startTime:"+dateFormat.format(new Date())+" - 执行即时通用户启停用接口！－－－－－－－＞");
		//返回值
		String isReturn = "OK";
		try {
			JSONObject json = JSONObject.fromObject(user);
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("jsonString", URLEncoder
						.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(params);
			if (log.isDebugEnabled()) {
				log.debug(rs);
			}
			//判断当前返回数据是否访问受限!
			String[] str = rs.split("访问受限");
			//根据截取返回数据判断是否访问受限!
			if (str.length>1) {
				return "【即时通接口错误：链接异常】";
			}
			Integer status = Integer.valueOf(rs);
			if (status <= 0) {
				switch (status) {
				case 0:
					isReturn = "【即时通接口错误：json格式有误】";
					break;
				case -1:
					isReturn = "【即时通接口错误：帐号提供有误】";
					break;
				}
			}

			System.out.println("<－－－－－－－endTime:"+dateFormat.format(new Date())+" - 执行即时通用户启停用结果返回值 : ["+status+"] "+isReturn+"－－－－－－－>");
		} catch (UnsupportedEncodingException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (ConnectException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (IOException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		}
		return isReturn;
	}*/
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 */
	public String delete(UserDelete user) {
		System.out.println("＜－－－－－－－startTime:"+dateFormat.format(new Date())+" - 执行即时通删除用户接口！－－－－－－－＞");
		//返回值
		String isReturn = "OK";
		try {
			JSONObject json = JSONObject.fromObject(user);
			Map<String, Object> params = new HashMap<String, Object>();
				params.put("jsonString", URLEncoder
						.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(read.getInterfaceUrl(),params);
			if (log.isDebugEnabled()) {
				log.debug(rs);
			}
			//判断当前返回数据是否访问受限!
			String[] str = rs.split("访问受限");
			//根据截取返回数据判断是否访问受限!
			if (str.length>1) {
				return "【即时通接口错误：链接异常】";
			}
			Integer status = Integer.valueOf(rs);
			if (status <= 0) {
				switch (status) {
				case -1:
					isReturn = "【即时通接口错误：没有提供相应的参数】";
					break;
				case -2:
					isReturn = "【即时通接口错误：帐号提供有误】";
					break;
				default:
					isReturn = "【即时通接口错误：没有提供相应的参数】";
					break;
				}
			}
			System.out.println("<－－－－－－－endTime:"+dateFormat.format(new Date())+" - 执行即时通删除用户结果返回值 : ["+status+"] "+isReturn+"－－－－－－－>");
		} catch (UnsupportedEncodingException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (ConnectException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		} catch (IOException e) {
			isReturn = "【即时通接口错误：链接异常】";
			e.printStackTrace();
		}
		return isReturn;
	}
	/**
	 * 得到用户未读信息数
	 * @param user
	 * @return
	 */
	/*public Integer getUnreadMsgCount(UserUnreadMsgCount msg) {
		Integer status =0;
		try {
			JSONObject json = JSONObject.fromObject(msg);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("jsonString", URLEncoder
					.encode(json.toString(), "UTF-8"));
			String rs = postReturnString(params);
			if (log.isDebugEnabled()) {
				log.debug(rs);
			}
			status = Integer.valueOf(rs);
			System.out.println("status=="+status);
			if (status >= 0) {
				return status;
			}else{
				return 0;
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return status;
	}*/


	public static UserOperator getInstance() {
		return UserOperatorHolder.userOperator;
	}
}
